/**
 * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 */

package com.hellodoctor.slayer.model;

import com.hellodoctor.slayer.service.ClpSerializer;
import com.hellodoctor.slayer.service.ProfileLocalServiceUtil;

import com.liferay.portal.kernel.bean.AutoEscapeBeanHandler;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.util.ProxyUtil;
import com.liferay.portal.kernel.util.StringBundler;
import com.liferay.portal.model.BaseModel;
import com.liferay.portal.model.impl.BaseModelImpl;
import com.liferay.portal.util.PortalUtil;

import java.io.Serializable;

import java.lang.reflect.Method;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Shahbaj
 */
public class ProfileClp extends BaseModelImpl<Profile> implements Profile {
	public ProfileClp() {
	}

	@Override
	public Class<?> getModelClass() {
		return Profile.class;
	}

	@Override
	public String getModelClassName() {
		return Profile.class.getName();
	}

	@Override
	public long getPrimaryKey() {
		return _profileId;
	}

	@Override
	public void setPrimaryKey(long primaryKey) {
		setProfileId(primaryKey);
	}

	@Override
	public Serializable getPrimaryKeyObj() {
		return _profileId;
	}

	@Override
	public void setPrimaryKeyObj(Serializable primaryKeyObj) {
		setPrimaryKey(((Long)primaryKeyObj).longValue());
	}

	@Override
	public Map<String, Object> getModelAttributes() {
		Map<String, Object> attributes = new HashMap<String, Object>();

		attributes.put("profileId", getProfileId());
		attributes.put("userId", getUserId());
		attributes.put("isDoctor", getIsDoctor());
		attributes.put("speciality", getSpeciality());
		attributes.put("qualification", getQualification());
		attributes.put("practicingSince", getPracticingSince());
		attributes.put("createDate", getCreateDate());
		attributes.put("modifiedDate", getModifiedDate());

		return attributes;
	}

	@Override
	public void setModelAttributes(Map<String, Object> attributes) {
		Long profileId = (Long)attributes.get("profileId");

		if (profileId != null) {
			setProfileId(profileId);
		}

		Long userId = (Long)attributes.get("userId");

		if (userId != null) {
			setUserId(userId);
		}

		Boolean isDoctor = (Boolean)attributes.get("isDoctor");

		if (isDoctor != null) {
			setIsDoctor(isDoctor);
		}

		Integer speciality = (Integer)attributes.get("speciality");

		if (speciality != null) {
			setSpeciality(speciality);
		}

		Integer qualification = (Integer)attributes.get("qualification");

		if (qualification != null) {
			setQualification(qualification);
		}

		Integer practicingSince = (Integer)attributes.get("practicingSince");

		if (practicingSince != null) {
			setPracticingSince(practicingSince);
		}

		Date createDate = (Date)attributes.get("createDate");

		if (createDate != null) {
			setCreateDate(createDate);
		}

		Date modifiedDate = (Date)attributes.get("modifiedDate");

		if (modifiedDate != null) {
			setModifiedDate(modifiedDate);
		}
	}

	@Override
	public long getProfileId() {
		return _profileId;
	}

	@Override
	public void setProfileId(long profileId) {
		_profileId = profileId;

		if (_profileRemoteModel != null) {
			try {
				Class<?> clazz = _profileRemoteModel.getClass();

				Method method = clazz.getMethod("setProfileId", long.class);

				method.invoke(_profileRemoteModel, profileId);
			}
			catch (Exception e) {
				throw new UnsupportedOperationException(e);
			}
		}
	}

	@Override
	public long getUserId() {
		return _userId;
	}

	@Override
	public void setUserId(long userId) {
		_userId = userId;

		if (_profileRemoteModel != null) {
			try {
				Class<?> clazz = _profileRemoteModel.getClass();

				Method method = clazz.getMethod("setUserId", long.class);

				method.invoke(_profileRemoteModel, userId);
			}
			catch (Exception e) {
				throw new UnsupportedOperationException(e);
			}
		}
	}

	@Override
	public String getUserUuid() throws SystemException {
		return PortalUtil.getUserValue(getUserId(), "uuid", _userUuid);
	}

	@Override
	public void setUserUuid(String userUuid) {
		_userUuid = userUuid;
	}

	@Override
	public boolean getIsDoctor() {
		return _isDoctor;
	}

	@Override
	public boolean isIsDoctor() {
		return _isDoctor;
	}

	@Override
	public void setIsDoctor(boolean isDoctor) {
		_isDoctor = isDoctor;

		if (_profileRemoteModel != null) {
			try {
				Class<?> clazz = _profileRemoteModel.getClass();

				Method method = clazz.getMethod("setIsDoctor", boolean.class);

				method.invoke(_profileRemoteModel, isDoctor);
			}
			catch (Exception e) {
				throw new UnsupportedOperationException(e);
			}
		}
	}

	@Override
	public int getSpeciality() {
		return _speciality;
	}

	@Override
	public void setSpeciality(int speciality) {
		_speciality = speciality;

		if (_profileRemoteModel != null) {
			try {
				Class<?> clazz = _profileRemoteModel.getClass();

				Method method = clazz.getMethod("setSpeciality", int.class);

				method.invoke(_profileRemoteModel, speciality);
			}
			catch (Exception e) {
				throw new UnsupportedOperationException(e);
			}
		}
	}

	@Override
	public int getQualification() {
		return _qualification;
	}

	@Override
	public void setQualification(int qualification) {
		_qualification = qualification;

		if (_profileRemoteModel != null) {
			try {
				Class<?> clazz = _profileRemoteModel.getClass();

				Method method = clazz.getMethod("setQualification", int.class);

				method.invoke(_profileRemoteModel, qualification);
			}
			catch (Exception e) {
				throw new UnsupportedOperationException(e);
			}
		}
	}

	@Override
	public int getPracticingSince() {
		return _practicingSince;
	}

	@Override
	public void setPracticingSince(int practicingSince) {
		_practicingSince = practicingSince;

		if (_profileRemoteModel != null) {
			try {
				Class<?> clazz = _profileRemoteModel.getClass();

				Method method = clazz.getMethod("setPracticingSince", int.class);

				method.invoke(_profileRemoteModel, practicingSince);
			}
			catch (Exception e) {
				throw new UnsupportedOperationException(e);
			}
		}
	}

	@Override
	public Date getCreateDate() {
		return _createDate;
	}

	@Override
	public void setCreateDate(Date createDate) {
		_createDate = createDate;

		if (_profileRemoteModel != null) {
			try {
				Class<?> clazz = _profileRemoteModel.getClass();

				Method method = clazz.getMethod("setCreateDate", Date.class);

				method.invoke(_profileRemoteModel, createDate);
			}
			catch (Exception e) {
				throw new UnsupportedOperationException(e);
			}
		}
	}

	@Override
	public Date getModifiedDate() {
		return _modifiedDate;
	}

	@Override
	public void setModifiedDate(Date modifiedDate) {
		_modifiedDate = modifiedDate;

		if (_profileRemoteModel != null) {
			try {
				Class<?> clazz = _profileRemoteModel.getClass();

				Method method = clazz.getMethod("setModifiedDate", Date.class);

				method.invoke(_profileRemoteModel, modifiedDate);
			}
			catch (Exception e) {
				throw new UnsupportedOperationException(e);
			}
		}
	}

	public BaseModel<?> getProfileRemoteModel() {
		return _profileRemoteModel;
	}

	public void setProfileRemoteModel(BaseModel<?> profileRemoteModel) {
		_profileRemoteModel = profileRemoteModel;
	}

	public Object invokeOnRemoteModel(String methodName,
		Class<?>[] parameterTypes, Object[] parameterValues)
		throws Exception {
		Object[] remoteParameterValues = new Object[parameterValues.length];

		for (int i = 0; i < parameterValues.length; i++) {
			if (parameterValues[i] != null) {
				remoteParameterValues[i] = ClpSerializer.translateInput(parameterValues[i]);
			}
		}

		Class<?> remoteModelClass = _profileRemoteModel.getClass();

		ClassLoader remoteModelClassLoader = remoteModelClass.getClassLoader();

		Class<?>[] remoteParameterTypes = new Class[parameterTypes.length];

		for (int i = 0; i < parameterTypes.length; i++) {
			if (parameterTypes[i].isPrimitive()) {
				remoteParameterTypes[i] = parameterTypes[i];
			}
			else {
				String parameterTypeName = parameterTypes[i].getName();

				remoteParameterTypes[i] = remoteModelClassLoader.loadClass(parameterTypeName);
			}
		}

		Method method = remoteModelClass.getMethod(methodName,
				remoteParameterTypes);

		Object returnValue = method.invoke(_profileRemoteModel,
				remoteParameterValues);

		if (returnValue != null) {
			returnValue = ClpSerializer.translateOutput(returnValue);
		}

		return returnValue;
	}

	@Override
	public void persist() throws SystemException {
		if (this.isNew()) {
			ProfileLocalServiceUtil.addProfile(this);
		}
		else {
			ProfileLocalServiceUtil.updateProfile(this);
		}
	}

	@Override
	public Profile toEscapedModel() {
		return (Profile)ProxyUtil.newProxyInstance(Profile.class.getClassLoader(),
			new Class[] { Profile.class }, new AutoEscapeBeanHandler(this));
	}

	@Override
	public Object clone() {
		ProfileClp clone = new ProfileClp();

		clone.setProfileId(getProfileId());
		clone.setUserId(getUserId());
		clone.setIsDoctor(getIsDoctor());
		clone.setSpeciality(getSpeciality());
		clone.setQualification(getQualification());
		clone.setPracticingSince(getPracticingSince());
		clone.setCreateDate(getCreateDate());
		clone.setModifiedDate(getModifiedDate());

		return clone;
	}

	@Override
	public int compareTo(Profile profile) {
		long primaryKey = profile.getPrimaryKey();

		if (getPrimaryKey() < primaryKey) {
			return -1;
		}
		else if (getPrimaryKey() > primaryKey) {
			return 1;
		}
		else {
			return 0;
		}
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}

		if (!(obj instanceof ProfileClp)) {
			return false;
		}

		ProfileClp profile = (ProfileClp)obj;

		long primaryKey = profile.getPrimaryKey();

		if (getPrimaryKey() == primaryKey) {
			return true;
		}
		else {
			return false;
		}
	}

	@Override
	public int hashCode() {
		return (int)getPrimaryKey();
	}

	@Override
	public String toString() {
		StringBundler sb = new StringBundler(17);

		sb.append("{profileId=");
		sb.append(getProfileId());
		sb.append(", userId=");
		sb.append(getUserId());
		sb.append(", isDoctor=");
		sb.append(getIsDoctor());
		sb.append(", speciality=");
		sb.append(getSpeciality());
		sb.append(", qualification=");
		sb.append(getQualification());
		sb.append(", practicingSince=");
		sb.append(getPracticingSince());
		sb.append(", createDate=");
		sb.append(getCreateDate());
		sb.append(", modifiedDate=");
		sb.append(getModifiedDate());
		sb.append("}");

		return sb.toString();
	}

	@Override
	public String toXmlString() {
		StringBundler sb = new StringBundler(28);

		sb.append("<model><model-name>");
		sb.append("com.hellodoctor.slayer.model.Profile");
		sb.append("</model-name>");

		sb.append(
			"<column><column-name>profileId</column-name><column-value><![CDATA[");
		sb.append(getProfileId());
		sb.append("]]></column-value></column>");
		sb.append(
			"<column><column-name>userId</column-name><column-value><![CDATA[");
		sb.append(getUserId());
		sb.append("]]></column-value></column>");
		sb.append(
			"<column><column-name>isDoctor</column-name><column-value><![CDATA[");
		sb.append(getIsDoctor());
		sb.append("]]></column-value></column>");
		sb.append(
			"<column><column-name>speciality</column-name><column-value><![CDATA[");
		sb.append(getSpeciality());
		sb.append("]]></column-value></column>");
		sb.append(
			"<column><column-name>qualification</column-name><column-value><![CDATA[");
		sb.append(getQualification());
		sb.append("]]></column-value></column>");
		sb.append(
			"<column><column-name>practicingSince</column-name><column-value><![CDATA[");
		sb.append(getPracticingSince());
		sb.append("]]></column-value></column>");
		sb.append(
			"<column><column-name>createDate</column-name><column-value><![CDATA[");
		sb.append(getCreateDate());
		sb.append("]]></column-value></column>");
		sb.append(
			"<column><column-name>modifiedDate</column-name><column-value><![CDATA[");
		sb.append(getModifiedDate());
		sb.append("]]></column-value></column>");

		sb.append("</model>");

		return sb.toString();
	}

	private long _profileId;
	private long _userId;
	private String _userUuid;
	private boolean _isDoctor;
	private int _speciality;
	private int _qualification;
	private int _practicingSince;
	private Date _createDate;
	private Date _modifiedDate;
	private BaseModel<?> _profileRemoteModel;
}